/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-25
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Coupon;
import com.rzico.basics.entity.CouponTarget;
import com.rzico.basics.entity.Product;
import com.rzico.basics.mapper.CouponMapper;
import com.rzico.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * <pre>
 * 优惠券业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class CouponService extends BaseServiceImpl<Coupon, String> {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponTargetService couponTargetService;

    @Override
    public BaseMapper<Coupon, String> getMapper() {
        return couponMapper;
    }



    /**
     * 可执行级联查询
     * @param params
     * @return
     */
    public List<Coupon> selectEffectiveList(Map<String, Object> params){
        return couponMapper.selectEffectiveList(params);
    }

    @Transactional(rollbackFor = Exception.class)
    public Coupon save(Coupon coupon) {
        coupon.setDeleted(false);
        super.insertUseGeneratedKeys(coupon);

        for (CouponTarget target:coupon.getTargets()) {
            target.setCouponId(coupon.getId());
            couponTargetService.insert(target);
        }


        return coupon;

    }

    @Transactional(rollbackFor = Exception.class)
    public Coupon update(Coupon coupon) {
        super.updateByPrimaryKeySelective(coupon);

        CouponTarget tg = new CouponTarget();
        tg.setCouponId(coupon.getId());
        couponTargetService.delete(tg);

        for (CouponTarget target:coupon.getTargets()) {
            target.setCouponId(coupon.getId());
            couponTargetService.insert(target);
        }

        return coupon;
    }

    public Boolean checkExists(Product product,Long id) {
        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",product.getEnterpriseId());
        params.put("productId",product.getId());
        params.put("type",3);
        List<Coupon> couponList = couponMapper.selectList(params);
        for (Coupon coupon:couponList) {
            if (id==null) {
                return true;
            }
            if (!coupon.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }


    @Transactional(rollbackFor = Exception.class)
    public Coupon create(Product product) {
        Coupon coupon = new Coupon();

        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",product.getEnterpriseId());
        params.put("productId",product.getId());
        params.put("type",3);
        List<Coupon> couponList = couponMapper.selectList(params);
        if (couponList.size()>0) {
            coupon = couponList.get(0);
            return coupon;
        }

        coupon.setProductId(product.getId());
        coupon.setMode(0);
        coupon.setAmount(product.getPrice());
        coupon.setBeginDate(new Date());
        coupon.setEndDate(DateUtils.addDate(new Date(), Calendar.YEAR,30));
        coupon.setEnterpriseId(product.getEnterpriseId());
        coupon.setGoods(product.getGoodsId());
        coupon.setName(product.getName());
        coupon.setMinimumPrice(BigDecimal.ZERO);
        coupon.setType(3);
        coupon.setSubType(0);
        coupon.setStock(9999999L);
        coupon.setMaxgiven(0L);
        coupon.setScope(0);
        coupon.setStatus(1);
        coupon.setTargetType(5);
        coupon.setQuantity(1L);
        coupon.setIntroduction("无门槛兑换券");
        coupon.setDeleted(false);
        super.insertUseGeneratedKeys(coupon);

        return coupon;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object[] ids) {
        int rw = 0;
        for (Object id:ids) {
            Coupon coupon = selectByPrimaryKey(id);
            coupon.setDeleted(true);
            rw = rw + super.updateByPrimaryKeySelective(coupon);
        }
        return rw;
    }

}
